from typing import *
from math import inf


class Solution:
    def minimumAddedInteger(self, nums1: List[int], nums2: List[int]) -> int:
        nums1.sort()
        nums2.sort()
        ans = inf
        for i in range(3):
            d = nums2[0] - nums1[i]
            jump = 0
            j = 0
            for k in range(i, len(nums1)):
                if nums2[j] - nums1[k] != d:
                    jump += 1
                else:
                    j += 1
                if i + jump > 2 or j == len(nums2):
                    break
            if i + jump <= 2:
                ans = min(ans, d)

        return ans


s = Solution()
print(s.minimumAddedInteger([4, 20, 16, 12, 8], [14, 18, 10]))
print(s.minimumAddedInteger([7, 2, 6, 8, 7], [7, 6, 5]))
